package com.avic.rong.questionOrAnswer.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.questionOrAnswer.bean.GetSupplyBean;
import com.avic.rong.questionOrAnswer.bean.TrainBean;
import com.avic.rong.utils.GlideUtils;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.example.minato.minastore.utils.AppUtils;

import java.util.List;

/**
 * Created by XinEr on 2019/8/27.
 * Describe:
 */
public class TrainListAdapter extends RecyclerView.Adapter<TrainListAdapter.ViewHolder> {

    private Context context;
    private List<TrainBean.CoursesListBean> listData;
    private String fileServer;


    public String getFileServer() {
        return fileServer;
    }

    public void setFileServer(String fileServer) {
        this.fileServer = fileServer;
    }


    public TrainListAdapter(Context context, List<TrainBean.CoursesListBean> listData) {
        this.context = context;
        this.listData = listData;


    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.train_list_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        TrainBean.CoursesListBean coursesListBean = listData.get(i);
        //TODO 返回数据缺少字段
//        GlideUtils.loaderImage(context,getSupplyBean.get);
//            viewHolder.tv_name.setText();

        if (!TextUtils.isEmpty(((PersonalHomeActivity) context).getLOGO())) {
            GlideUtils.loaderHanldeRoundImage(context, ((PersonalHomeActivity) context).getLOGO(), viewHolder.ic_photo, 80);
        } else {
            GlideUtils.loaderHanldeRoundImageResource(context, R.mipmap.ic_default_head, viewHolder.ic_photo, 80);
//            Glide.with(context).load(R.mipmap.ic_default_head).into(viewHolder.ic_photo);
        }

        if (1 == ((PersonalHomeActivity) context).getISV()) {
            viewHolder.iv_attestation.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(((PersonalHomeActivity) context).getNAME())) {
            viewHolder.tv_name.setText(((PersonalHomeActivity) context).getNAME());
        }
        viewHolder.tv_date.setText(coursesListBean.getCreateTime());
        viewHolder.tv_course_name.setText(coursesListBean.getName());
        if (coursesListBean.getType() == 2 && !TextUtils.isEmpty(coursesListBean.getType() + "")) {
            viewHolder.tv_course_start_time.setText("开课时间：" + coursesListBean.getStartTime());
            viewHolder.tv_course_address.setText("开课地址：" + coursesListBean.getCity());
            viewHolder.tv_course_start_time.setVisibility(View.VISIBLE);
            viewHolder.tv_course_address.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(coursesListBean.getCover())) {
//                Glide.with(context).load(R.mipmap.offline_default).into(viewHolder.iv_course_pic);
                GlideUtils.loaderHanldeRoundImageResource(context, R.mipmap.offline_default, viewHolder.iv_course_pic, 10);
            } else {
                GlideUtils.loaderHanldeRoundImage(context, getFileServer() + coursesListBean.getCover(), viewHolder.iv_course_pic, 10);
            }

        } else if (coursesListBean.getType() == 0 || coursesListBean.getType() == 1 & !TextUtils.isEmpty(coursesListBean.getType() + "")) {
            viewHolder.tv_course_time.setText("时长：" + coursesListBean.getTotalTime());
            viewHolder.tv_course_num.setText("课时：共" + coursesListBean.getCoursesHourCount() + "课时");
            viewHolder.tv_course_time.setVisibility(View.VISIBLE);
            viewHolder.tv_course_num.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(coursesListBean.getCover())) {
//                Glide.with(context).load(R.mipmap.online_default).into(viewHolder.iv_course_pic);
                GlideUtils.loaderHanldeRoundImageResource(context, R.mipmap.online_default, viewHolder.iv_course_pic, 10);
            } else {
                GlideUtils.loaderHanldeRoundImage(context, getFileServer() + coursesListBean.getCover(), viewHolder.iv_course_pic, 10);
            }

        }


        viewHolder.tv_course_price.setText("¥" + coursesListBean.getPrice());
        viewHolder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (coursesListBean.getType() == 2 && !TextUtils.isEmpty(coursesListBean.getType() + "")) {
                    if (TextUtils.isEmpty(coursesListBean.getCover())) {
                        context.startActivity(CourseDetailActivity.getIntent(context, "" + coursesListBean.getType() + coursesListBean.getId(), "", coursesListBean.getName()));
                    } else {
                        context.startActivity(CourseDetailActivity.getIntent(context, "" + coursesListBean.getType() + coursesListBean.getId(), getFileServer() + coursesListBean.getCover(), coursesListBean.getName()));
                    }

                } else if (coursesListBean.getType() == 0 || coursesListBean.getType() == 1 & !TextUtils.isEmpty(coursesListBean.getType() + "")) {
                    if (TextUtils.isEmpty(coursesListBean.getCover())) {
                        context.startActivity(CourseDetailActivity.getIntent(context, "" + coursesListBean.getType() + coursesListBean.getId(), "", coursesListBean.getName()));
                    } else {
                        context.startActivity(CourseDetailActivity.getIntent(context, "" + coursesListBean.getType() + coursesListBean.getId() + "", getFileServer() + coursesListBean.getCover(), coursesListBean.getName()));
                    }

                }
            }
        });
        viewHolder.setIsRecyclable(false);
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ic_photo;
        private TextView tv_name;
        private TextView tv_date;
        private TextView tv_sign;
        private ImageView iv_course_pic;
        private TextView tv_course_name;
        private TextView tv_course_num;
        private TextView tv_course_time;
        private TextView tv_course_start_time;
        private TextView tv_course_address;
        private TextView tv_course_price;
        private LinearLayout ll_root;
        private ImageView iv_attestation;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ic_photo = itemView.findViewById(R.id.ic_photo);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_sign = itemView.findViewById(R.id.tv_sign);
            iv_course_pic = itemView.findViewById(R.id.iv_course_pic);
            tv_course_name = itemView.findViewById(R.id.tv_course_name);
            tv_course_num = itemView.findViewById(R.id.tv_course_num);
            tv_course_time = itemView.findViewById(R.id.tv_course_time);
            tv_course_start_time = itemView.findViewById(R.id.tv_course_start_time);
            tv_course_address = itemView.findViewById(R.id.tv_course_address);
            tv_course_price = itemView.findViewById(R.id.tv_course_price);
            ll_root = itemView.findViewById(R.id.ll_root);
            iv_attestation = itemView.findViewById(R.id.iv_attestation);
        }
    }
}
